package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.weicheng.WeiChengRecord;
import com.ucredit.babel.common.model.third.weicheng.WeiChengData;
import com.weicai.caesar.CaesarUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 卫诚征信
 *
 * @author xj
 */

public class WeichengRecordParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 卫诚征信
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        WeiChengRecord obj = (WeiChengRecord) model;
        WeiChengData data = obj.getData();

        StringBuilder sb = getBaseInfo(obj, successful);
        String sertiCode = "";
        String name = "";
        if (data != null && StringUtils.isNotBlank(data.getSertiCode())) {
            sertiCode = CaesarUtil.isEncrypted(data.getSertiCode()) ? data.getSertiCode() : CaesarUtil.encode(data.getSertiCode());
        }
        if (data != null && StringUtils.isNotBlank(data.getName())) {
            name = CaesarUtil.isEncrypted(data.getName()) ? data.getName() : CaesarUtil.encode(data.getName());
        }
        //返回数据
        sb.append(data == null ? "" : data.getLevel()).append(CRT001);
        //订单号
        sb.append(data == null ? "" : data.getQueryOrderNumber()).append(CRT001);
        //请求IP
        sb.append(data == null ? "" : data.getRequestIp()).append(CRT001);
        //身份证号
        sb.append(sertiCode).append(CRT001);
        //姓名
        sb.append(name).append(CRT001);
        StringBuilder txSb = getBaseInfo(obj);
        key.set(getKey());
        val.set((sb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_weicheng_record_inc";
    }

    @Override
    public String getType() {
        return RecordType.WEICHENG_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "卫诚征信";
    }
}
